<?php
/**
 * 后台专属函数
 */
// ...

/**
 * 通过userid 获取logo
 */
function lg_admin_logo($user_id, $is_pic = 0)
{
	if ($is_pic == 0) {
		$url = '/Public/Assets/avatars/no.png';
	} else {
		$url = '/Uploads/Admin/Manager/Pic/'.$user_id.'.jpeg';
	}
	return $url;
}

/**
 * 生成随机码
 * @param  int $length
 * @param  int $type
 * @return string
 */
function rand_code($length, $type) {
	$rand_factor = array("0123456789",
			"abcdefghijklmnopqrstuvwxyz",
			"ABCDEFGHIJKLMNOPQRSTUVWXYZ",	
			"~@#$%^&*(){}[]|");

    if (($type < 0 && $type != -1) || $type > 4) {
        $type = 0;
    }

    if (0 == $type) {
        array_pop($rand_factor);
        $rand_src = implode("", $rand_factor);
    } else if (-1 == $type) {
        $rand_src = implode("", $rand_factor);
    } else {
        $rand_src = $rand_factor[$type];
    }

    $code = '';
    $count = strlen($rand_src) - 1;
    for ($i = 0; $i < $length; $i++) {
        $code .= $rand_src[rand(0, $count)];
    }

    return $code;
}

/**
 * 生成编辑器
 * @param  string $name   文本域name
 * @param  string $value  文本域value
 * @param  int    $rows   文本域rows
 * @param  int    $cols   文本域cols
 * @param  string $type   编辑器类型
 * @return string
 */
function genEditor($name, $value, $cols, $rows, $type = 'simple') {
	$id = rand_code(8);
	$html = "<textarea name='{$name}' id='{$id}' "
	. "rows='{$rows}' cols='{$cols}' ";

	if ('simple' == $type) {
			$js = "<script type='text/javascript'>$(function(){KindEditor.ready(function(K) {K.create('#{$id}',{resizeType:1,items:['fontname','fontsize','|','forecolor','hilitecolor','bold','italic','underline','removeformat','|','justifyleft','justifycenter','justifyright','insertorderedlist','insertunorderedlist','|','emoticons','image','link'],afterBlur:function(){this.sync();}});});});</script>";
	} else {
	$js = "<script type='text/javascript'>$(function(){KindEditor.ready(function(K) {K.create('#{$id}',{resizeType:1,afterBlur:function(){this.sync();}});});});</script>";
	}

	if (isset($value) && !empty($value)) {
	$html .= ">{$value}</textarea>";
	} else {
	$html .= "></textarea>";
	}

	return $html . $js;
	}
